package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemCatMapper;
import com.usian.mapper.TbItemParamMapper;
import com.usian.pojo.TbItemCat;
import com.usian.pojo.TbItemParam;
import com.usian.pojo.TbItemParamExample;
import com.usian.service.TBItemParamService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 韩耀东
 * @date 2021/1/6 10:45
 */
@Service
public class TBItemParamServiceImpl implements TBItemParamService {

    @Autowired
    private TbItemParamMapper itemParamMapper;
    @Autowired
    private TbItemCatMapper itemCatMapper;

    /**
     * @return com.usian.pojo.TbItemParam
     * @Author 韩耀东
     * @Description //TODO 根据商品分类 ID 查询规格参数模板
     * @Date 2021/1/6 10:46
     * @Param [itemCatId]
     **/
    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample itemParamExample = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = itemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(itemParamExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * @return com.usian.utils.PageResult
     * @Author 韩耀东
     * @Description //TODO 查询所有商品规格模板
     * @Date 2021/1/7 19:49
     * @Param [page, rows]
     **/
    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemParamExample example = new TbItemParamExample();
        example.setOrderByClause("updated DESC");
        List<TbItemParam> list = this.itemParamMapper.selectByExampleWithBLOBs(example);
        for (TbItemParam tbItemParam : list) {
            TbItemCat tbItemCat = itemCatMapper.selectByPrimaryKey(tbItemParam.getItemCatId());
            tbItemParam.setParamData(tbItemCat.getName());
        }
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(list);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(page);
        pageResult.setResult(pageInfo.getList());
        pageResult.setTotalPage(Long.valueOf(pageInfo.getPages()));
        return pageResult;
    }

    @Override
    public Integer deleteItemParamById(Long id) {
        return itemParamMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer insertItemParam(Long itemCatId, String paramData) {
        //1、判断该类别的商品是否有规格模板
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = tbItemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> itemParamList = itemParamMapper.selectByExample(tbItemParamExample);
        if (itemParamList.size() > 0) {
            return 0;
        }

        //2、保存规格模板
        Date date = new Date();
        TbItemParam tbItemParam = new TbItemParam();
        tbItemParam.setItemCatId(itemCatId);
        tbItemParam.setParamData(paramData);
        tbItemParam.setUpdated(date);
        tbItemParam.setCreated(date);
        return itemParamMapper.insertSelective(tbItemParam);
    }
}
